package com.chen.config;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Component
public class ConfirmRabbitListener {
    public static final String QUEUE_Name = "queue1";


   @RabbitListener(queues = {"queue.delay"})
   public void processDelayPluginMessage(String data,Message message, Channel channel) throws IOException {
       System.out.println("消息内容是:"+data);
       System.out.println("当前时间是:"+new SimpleDateFormat("HH:mm:ss").format(new Date()));
       //确认消
       channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
   }

    /**
     * 监听正常的队列
     */
//    @RabbitListener(queues = {"normal.queue"})
//    public void processMessageNormal(Message message, Channel channel) throws IOException {
//        // 监听正常队列，但是拒绝消息
//        System.out.println("[normal]消息接收到，但我拒绝");
//        //返回NACK拒绝
//        channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
//    }

    /**
     * 监听死信队列
     */
//    @RabbitListener(queues = {"dead.queue"})
//    public void processMessageDead(String dataString, Message message, Channel channel) throws IOException {
//        System.out.println("★[dead letter]dataString = " + dataString);
//        System.out.println("★[dead letter]我是死信监听方法，我接收到了死信消息");
//        //返回ACK接受
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//    }

//    @RabbitListener(queues = QUEUE_Name)
//    public void processMessageTestPreCatch(String data, Message message, Channel channel) throws IOException, InterruptedException {
//        //让处理的速度慢一点方便观察
//        TimeUnit.SECONDS.sleep(1);
//        //  还是采用手动确认
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//    }

//    @RabbitListener(queues = QUEUE_Name)
//    public void processMessage(String data, Message message, Channel channel) throws IOException {
//        //获取 deliveryTag 消息的唯一标识
//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        try {
//            //核心业务
//            System.out.println(data);
//            System.out.println(1/0);
//            //成功了返回AKC
//            //multiple: 是否批量处理数据
//            channel.basicAck(deliveryTag, false);
//        } catch (Exception e) {
//            //时候被重新投递过
//            Boolean redelivered = message.getMessageProperties().getRedelivered();
//            if (redelivered) {
//
//                //requeue  true,  broker会重新投放消息
//                // false  broker会丢掉这个消息
//                //被重新投递过直接丢弃
//                channel.basicNack(deliveryTag, false, false);
//
//            } else {
//                //失败了返回NACK
//                // 没被重新投递过重新投递
//                channel.basicNack(deliveryTag, false, true);
//            }
//        }
//
//    }
}
